﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using quanlynhasach.Linksql;

namespace quanlynhasach
{
    public partial class frmKhachHang : Form
    {
        KhachHangsql khachhangsql = new KhachHangsql();
        Connection db = new Connection();
        int Count;
        public frmKhachHang()
        {
            InitializeComponent();
        }

        private KhachHang getData()
        {
            KhachHang kh = new KhachHang();
            kh.MaKH = lbMaKhachHang.Text;
            kh.TenKH = textBoxHoTenKH.Text;
            kh.NgaySinhKH = dateTimeNgaysinhKH.Value.ToShortDateString();
            kh.SoCMNDKH = textBoxSoCMNDKH.Text;
            kh.SoDienThoaiKH = textBoxSoDTKH.Text;
            kh.DiaChiKH = textBoxDiaChiKH.Text;
            kh.EmailKH = textBoxEmailKH.Text;
            kh.GioiTinhKH = comboBoxGioiTinhKH.Text;
            return kh;
        }

        private void loadData()
        {
            DataTable KH = new DataTable();
            KH = khachhangsql.getKH();
            grdtNhanVien.DataSource = KH;
        }

        private void Enable(bool a)
        {
            grdtNhanVien.Enabled = !a;
            btnThemKH.Enabled = !a;
            btnSuaKH.Enabled = !a;
            btnXoaKH.Enabled = !a;
            btnLuuKH.Enabled = a;
            btnHuyKH.Enabled = a;
            textBoxHoTenKH.Enabled = a;
            textBoxDiaChiKH.Enabled = a;
            textBoxEmailKH.Enabled = a;
            dateTimeNgaysinhKH.Enabled = a;
            textBoxSoCMNDKH.Enabled = a;
            textBoxSoDTKH.Enabled = a;
            comboBoxGioiTinhKH.Enabled = a;
            if (lbMaKhachHang.Text.ToUpper() == frmMain.user.ToUpper())
                btnXoaKH.Enabled = false;
        }

        private void reset()
        {
            textBoxHoTenKH.Text = "";
            textBoxDiaChiKH.Text = "";
            textBoxEmailKH.Text = "";
            dateTimeNgaysinhKH.Text = "1/10/1994";
            textBoxSoCMNDKH.Text = "";
            textBoxSoDTKH.Text = "";
            comboBoxGioiTinhKH.Text = "Nam";
        }

        private string SearchDaTaGrid()
        {
            Count = 0;
            string MaKH = "";
            DataTable dt = new DataTable();//tao bang tam de luu
            dt = db.getDS("Select MaKH From KhachHang");
            for (int i = 1; true; i++)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (dt.Rows[j]["MaKH"].ToString() != "KH" + i)
                    {
                        Count++;//dem so lan khac
                    }
                    else
                    {
                        Count = 0;
                        break;
                    }
                }
                if (Count == dt.Rows.Count)// new so lan khac bang so hang cua bang, nghia la khong co dong nao trung thi tu dong add
                {
                    MaKH = "KH" + i;
                    break;
                }
            }
            return MaKH;
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 
            grdtNhanVien.DefaultCellStyle.ForeColor = Color.Blue;
            grdtNhanVien.AllowUserToAddRows = false;
            loadData();

        }

        private void btnThemKhachHang_Click(object sender, EventArgs e)
        {
            Enable(true);
            lbMaKhachHang.Text = SearchDaTaGrid();
            reset();
        }

        private void btnHuyKhachHang_Click(object sender, EventArgs e)
        {
            Enable(false);
            loadData();
        }



        private void grdtKhachHang_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int Rs = e.RowIndex;
            lbMaKhachHang.Text = grdtNhanVien.Rows[Rs].Cells["MaKH"].Value.ToString();
            textBoxHoTenKH.Text = grdtNhanVien.Rows[Rs].Cells["TenKH"].Value.ToString();
            dateTimeNgaysinhKH.Text = grdtNhanVien.Rows[Rs].Cells["NgaySinh"].Value.ToString();
            textBoxSoCMNDKH.Text = grdtNhanVien.Rows[Rs].Cells["SoCMND"].Value.ToString();
            textBoxSoDTKH.Text = grdtNhanVien.Rows[Rs].Cells["SoDT"].Value.ToString();
            textBoxDiaChiKH.Text = grdtNhanVien.Rows[Rs].Cells["DiaChi"].Value.ToString();
            textBoxEmailKH.Text = grdtNhanVien.Rows[Rs].Cells["Email"].Value.ToString();
            comboBoxGioiTinhKH.Text = grdtNhanVien.Rows[Rs].Cells["GioiTinh"].Value.ToString();
            if (lbMaKhachHang.Text.ToUpper() == frmMain.user.ToUpper())
                btnXoaKH.Enabled = false;
            else
                btnXoaKH.Enabled = true;
        }

        private void btnSuaKhachHang_Click(object sender, EventArgs e)
        {
            Enable(true);
        }

        private void btnXoaKhachHang_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc là xóa nhân viên " + lbMaKhachHang.Text +
                " này không ?. Nếu xóa, tất cả dữ liệu liên quan sẽ bị xóa theo.", "Xác nhận",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                if (khachhangsql.XoaKH(lbMaKhachHang.Text))
                    MessageBox.Show("Xóa thành công !");
            loadData();
        }

        private void btnLuuKhachHang_Click(object sender, EventArgs e)
        {
            KhachHang kh = getData();
            if (khachhangsql.check(kh.MaKH))
                if (khachhangsql.SuaKH(kh))
                    MessageBox.Show("Cập nhật thành công");
                else
                    MessageBox.Show("Xem lại !");
            else
                if (khachhangsql.ThemKH(kh))
                    MessageBox.Show("Thêm thành công !");
                else
                    MessageBox.Show("Xem lại !");
            loadData();
            Enable(false);
        }
    }
}
